package cn.com.twoke.app.pin_dou_dou.color;

import cn.com.twoke.app.pin_dou_dou.utils.ColorLabUtils;

import java.awt.*;

/**
 * 计算 ΔE*94 色差（CIE94标准）
 * 比 CIE76 更精确，考虑了感知均匀性
 */
public class CIE_94 implements ColorDifferenceCalculator{

    @Override
    public double deltaE(Color color1, Color color2) {
        double[] lab1 = ColorLabUtils.rgbToLab(color1);
        double[] lab2 = ColorLabUtils.rgbToLab(color2);

        double dl = lab1[0] - lab2[0];
        double da = lab1[1] - lab2[1];
        double db = lab1[2] - lab2[2];

        double c1 = Math.sqrt(lab1[1] * lab1[1] + lab1[2] * lab1[2]);
        double c2 = Math.sqrt(lab2[1] * lab2[1] + lab2[2] * lab2[2]);
        double dc = c1 - c2;

        double dh = Math.sqrt(da * da + db * db - dc * dc);
        if (Double.isNaN(dh)) {
            dh = 0;
        }

        // 权重系数（图形艺术应用）
        double kl = 1.0;
        double kc = 1.0;
        double kh = 1.0;

        double sl = 1.0;
        double sc = 1.0 + 0.045 * c1;
        double sh = 1.0 + 0.015 * c1;

        return Math.sqrt(
                Math.pow(dl / (kl * sl), 2) +
                        Math.pow(dc / (kc * sc), 2) +
                        Math.pow(dh / (kh * sh), 2)
        );
    }

}
